/************************************************************************************/
/*  Program name CAPFNDEXT_KFF_SEGMENT.sql                                          */
/*  Creater:  NIP Karen (PCCW - UniHub)                                             */
/*************   History   *************************                                */
/* 8 FEB, 2005   NIP Karen         First created                                    */
/************************************************************************************/
ALTER SESSION SET NLS_DATE_FORMAT='YYYYMMDD HH24:MI:SS';
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFNDEXT_KFF_SEGMENT';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	lv_FileName		VARCHAR2(100)	:=	'CAPFND_KFF_SEGMENT.dat';
	lv_PathName		VARCHAR2(100)	:=	'/tmp';
	lv_UTL_File		UTL_FILE.File_Type;

	lv_NGO_Code		VARCHAR2(100);
	lv_NGO_Count	NUMBER;

	CURSOR Cur_F(p_NGO VARCHAR2) IS
	SELECT 
		FST.ID_FLEX_STRUCTURE_CODE, 
		FSG.SEGMENT_NUM, 
		FSGT.LANGUAGE, 
		FSG.APPLICATION_COLUMN_NAME, 
		FSG.SEGMENT_NAME,
		FSGT.FORM_LEFT_PROMPT, 
		FSGT.FORM_ABOVE_PROMPT, 
		FSGT.DESCRIPTION,
		FSG.APPLICATION_COLUMN_INDEX_FLAG, 
		FSG.ENABLED_FLAG, 
		FSG.REQUIRED_FLAG, 
		FSG.DISPLAY_FLAG,
		FSG.DISPLAY_SIZE, 
		FSG.SECURITY_ENABLED_FLAG, 
		FSG.MAXIMUM_DESCRIPTION_LEN,
		FSG.CONCATENATION_DESCRIPTION_LEN, 
		FFVS.FLEX_VALUE_SET_NAME
	FROM   
		FND_ID_FLEX_STRUCTURES	FST, 
		FND_ID_FLEX_SEGMENTS	FSG, 
		FND_ID_FLEX_SEGMENTS_TL	FSGT, 
		FND_FLEX_VALUE_SETS		FFVS
	WHERE	FST.APPLICATION_ID	=	FSG.APPLICATION_ID
	AND		FST.ID_FLEX_CODE	=	FSG.ID_FLEX_CODE
	AND		FST.ID_FLEX_NUM		=	FSG.ID_FLEX_NUM
	AND		SUBSTR(FST.ID_FLEX_STRUCTURE_CODE,1,4)	=	p_NGO
	AND		FSG.APPLICATION_ID	=	FSGT.APPLICATION_ID
	AND		FSG.ID_FLEX_CODE	=	FSGT.ID_FLEX_CODE
	AND		FSG.ID_FLEX_NUM		=	FSGT.ID_FLEX_NUM
	AND		FSG.APPLICATION_COLUMN_NAME	=	FSGT.APPLICATION_COLUMN_NAME
	AND		FSG.FLEX_VALUE_SET_ID	=	FFVS.FLEX_VALUE_SET_ID(+)
	ORDER BY	FST.ID_FLEX_STRUCTURE_CODE, 
				FSG.SEGMENT_NUM, FSGT.LANGUAGE;

BEGIN
	lv_NGO_Code		:=	UPPER('&NGO_Code');

	SELECT	COUNT('A')
	INTO	lv_NGO_Count
	FROM	HR_ALL_ORGANIZATION_UNITS
	WHERE	SUBSTR(Name,4,4)	=	lv_NGO_Code;

	IF	lv_NGO_Count	>=	1	THEN
		lv_UTL_File		:=	UTL_FILE.FOpen(lv_PathName, lv_FileName, 'w');
		FOR C1 IN Cur_F(lv_NGO_Code) LOOP
			UTL_FILE.PUT_LINE(
				lv_UTL_File,
				C1.ID_FLEX_STRUCTURE_CODE||CHR(9)||
				C1.SEGMENT_NUM||CHR(9)||
				C1.LANGUAGE||CHR(9)||
				C1.SEGMENT_NAME||CHR(9)||
				C1.FORM_LEFT_PROMPT||CHR(9)||
				C1.FORM_ABOVE_PROMPT||CHR(9)||
				C1.APPLICATION_COLUMN_NAME||CHR(9)||
				C1.FLEX_VALUE_SET_NAME||CHR(9)||
				C1.DISPLAY_FLAG||CHR(9)||
				C1.ENABLED_FLAG||CHR(9)||
				C1.DESCRIPTION||CHR(9)||
				C1.APPLICATION_COLUMN_INDEX_FLAG||CHR(9)||
				C1.REQUIRED_FLAG||CHR(9)||
				C1.SECURITY_ENABLED_FLAG||CHR(9)||
				C1.DISPLAY_SIZE||CHR(9)||
				C1.MAXIMUM_DESCRIPTION_LEN||CHR(9)||
				C1.CONCATENATION_DESCRIPTION_LEN
			);

			lv_Scc_Count	:=	lv_Scc_Count	+	1;
		END LOOP;
		UTL_FILE.FClose(lv_UTL_File);
	ELSE
		lv_Err_Count	:=	lv_Err_Count + 1;
		lv_Err_Msg		:=	lv_NGO_Code||': No such NGO exists';
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);

	COMMIT;
END;
/
